<%-- 
    Document   : uploadaction
    Created on : Dec 16, 2009, 6:04:55 PM
    Author     : joselito
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="br.com.zumbits.avd.web.client.Conn" %>
<%@page import="java.io.*" %>
<%@page import="java.util.*" %>
<%@page import="org.apache.commons.fileupload.*" %>
<%@page import="org.apache.commons.fileupload.disk.*" %>
<%@page import="org.apache.commons.fileupload.servlet.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Another Disk Virtual</title>
        <%
            String buttonValue = session.getAttribute("buttonSend").toString();

            if (buttonValue.equals("Send")) {
                // Create a factory for disk-based file items
                FileItemFactory factory = new DiskFileItemFactory();

                // Create a new file upload handler
                ServletFileUpload upload = new ServletFileUpload(factory);
                List items;
                String filename = "temporario";
                try {
                    // Parse the request
                    items = upload.parseRequest(request);
                    filename = ((DiskFileItem) items.get(0)).getName();
                } catch (FileUploadException ex) {
                    System.out.println("ERRO: FILEUPLOADEXCEPTION");

                    return;
                }

                Iterator iter = items.iterator();
                while (iter.hasNext()) {
                    FileItem item = (FileItem) iter.next();

                    if (!item.isFormField()) {
                        Conn c = (Conn) request.getSession().getAttribute("conn");
                        String session_id = session.getAttribute("session_id").toString();
                        String currentPath = request.getParameter("currentPath");
                        String uploadedFileName = Conn.UPLOAD_TEMP_FILENAME + c.toString();
                        File uploadedFile = new File(uploadedFileName);
                        try {
                            item.write(uploadedFile);
                            String resultado = c.upload(session_id, currentPath, filename, "0", uploadedFileName);
                            response.getWriter().write(resultado);
                            System.out.println("RESULTADO UPLOAD: " + resultado);

                            if (resultado.equals(Conn.UPLOAD_SUCCESS)) {
                                request.getSession().setAttribute("message", "Arquivo enviado com sucesso");
                                response.sendRedirect("startpage.jsp?currentPath=" + currentPath);
                            } else if (resultado.equals(Conn.UPLOAD_NOT_LOGGED_IN)) {
                                request.getSession().setAttribute("message", Conn.MSG_NOT_LOGGED_IN);
                                response.sendRedirect("index.jsp?");
                            } else if (resultado.equals(Conn.UPLOAD_CONFLICT)) {
                                request.getSession().setAttribute("message", "A pasta j&aacute; cont&eacute;m um arquivo com o nome especificado.");
                                response.sendRedirect("startpage.jsp?currentPath=" + currentPath);
                            } else {
                                request.getSession().setAttribute("message", Conn.MSG_FAILURE);
                                response.sendRedirect("startpage.jsp?currentPath=" + currentPath);
                            }
                            return;
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            }
        %>
    </head>
    <body>
        <h1>Ops</h1>
    </body>
</html>
